// pages/courseDetail/courseDetail.js
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		c_id: "",
		s_id: "",
		poster: "",
		name: "",
		brief: "",
		coach: "",
		week: "",
		length: "",
		place: "",
		startTime: "",
		times: "",
		remainingTimes: "",
		signalHidden: true,
		signed: false,
		allowSign: false,
		day: ['', '一', '二', '三', '四', '五', '六', '日'],
		time1: ["", "8:50", "9:40", "10:40", "11:30", "14:00", "14:50", "15:45", "16:35", "17:30", "19:00", "19:50",
			"20:40"
		],
		time2: ["", "9:35", "10:25", "11:25", "12:15", "14:45", "15:35", "16:30", "17:20", "19:00", "19:45", "20:35",
			"21:25"
		],
	},


	leave: function() {
		wx.navigateTo({
			url: '../leaveRequest/leaveRequest?id=' + this.data.c_id + '&name=' + this.data.name +
				'&studentID=' + this.data.s_id
		})
	},

	sign: function() {
		var that = this;
		const db = wx.cloud.database();
		const CourseSales = db.collection('CourseSales');
		const Record = db.collection('Record');
		if (!this.data.signed) {
			CourseSales.where({
				courseId: that.data.c_id,
				studentID: that.data.s_id,
			}).get({
				success: res => {
					var temp = res.data[0].sign;
					var status = res.data[0].status;
					temp[status] = 1;
					CourseSales.where({
						courseId: that.data.c_id,
						studentID: that.data.s_id,
					}).update({
						data: {
							sign: temp
						},
						success: () => {
							that.setData({
								signed: true
							});
							console.log("签到成功");
						},
						fail: res => {
							console.log(res);
						}
					})
				}
			})
			//修改record表，增加signtime和courseID
			Record.where({
				studentID: that.data.s_id
			}).get({
				success: res => {
					//console.log(res.data[0]);
					var temp = res.data[0].signTime;
					var date = new Date();
					var today = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
					var index = -1; //初始，signTime记录里面没有今天的
					var repeat = false;
					//console.log(today); 
					for (let i = 0; i < temp.length; i++) {
						if (temp[i].day === today) {
							index = i;
							break;
						}
					}
					if (index === -1) { //没找到今天的记录
						temp.push({
							courseID: [that.data.c_id],
							day: today
						});
					} else { //找到了今天的记录
						for (let i = 0; i < temp[index].courseID.length; i++) //判断courseID有没有重复
						{
							if (that.data.c_id === temp[index].courseID[i]) {
								repeat = true;
								break;
							}
						}
						if (!repeat) {
							temp[index].courseID.push(that.data.c_id);
						}
					}
					Record.where({
						studentID: that.data.s_id
					}).update({
						data: {
							signTime: temp
						}
					})
				},
				fail: res => {
					console.log(res);
				}
			})
		}

		this.setData({
			signalHidden: false
		});
		setTimeout(function() {
			that.setData({
				signalHidden: true
				// signalShow:false
			});
		}, 1000);

	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		var that = this;
		this.setData({
			times: options.times,
			remainingTimes: options.remain,
			c_id: options.c_id,
			s_id: options.s_id,
			allowSign: Boolean(options.sign)
		})
		//加载数据库数据
		const db = wx.cloud.database();
		db.collection('Course').where({
			_id: options.c_id
		}).get({
			success: res => {
				that.setData({
					poster: res.data[0].posterURL,
					name: res.data[0].name,
					brief: res.data[0].brief,
					coach: res.data[0].coach,
					week: res.data[0].week,
					length: res.data[0].length,
					place: res.data[0].place,
					startTime: res.data[0].startTime,
				}, () => {
					//
				})
			},
			fail: res => {
				console.log(res);
				return;
			}
		})

	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
